package com.example.springboottools.utils.ruiyin;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.example.springboottools.utils.FileUtils;
import ink.rayin.htmladapter.base.PdfGenerator;
import ink.rayin.htmladapter.openhtmltopdf.service.PdfBoxGenerator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedHashSet;

public class RaYinPdfUtils {
    static Logger logger = LoggerFactory.getLogger(RaYinPdfUtils.class);
    static PdfGenerator pdfGenerator;
    static  {
        try {
            pdfGenerator = new PdfBoxGenerator();
           // URL fontDirUrl = RaYinPdfUtils.class.getClassLoader().getResource("font");
            //pdfGenerator.init(fontDirUrl.getPath());
            pdfGenerator.init();
        } catch (Exception e) {
            logger.error("初始化pdf生成器失败", e);
        }
    }

    public static void main(String[] args) {
        String fileHtmlStr = FileUtils.readBufReadFile("D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\新的模版修改\\标准模板\\V1\\S0X-V1.html");
       // String fileHtmlStr = HttpUtil.get("http://salesoss.stbchina.cn/test/pod_test/POD-H2.html/POD-H2.html");
        String fileJsonStr = FileUtils.readBufReadFile("D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\test\\POD_H1_data-2.json");
       // JSONObject jsonData = JSONObject.parseObject(fileJsonStr);
        String outputFile ="D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\test\\20250401-S0x-V1.pdf";
        LinkedHashSet<String> fontNames = pdfGenerator.getFontNames();
      //  String s = HttpUtil.get("http://salesoss.stbchina.cn/test/pod_test/POD-H2.html/POD-H2.html");
        String s = FileUtils.readBufReadFile("D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\新的模版修改\\标准模板\\V1\\S0X-V1.html");
        //赵璟嘉
        byte[] bytes = generatePdfSteamByHtmlStrAndData(s, JSONObject.parseObject(jsonData));
        String filePath = "D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\test\\aaa.pdf"; // 输出文件路径
        try (FileOutputStream fos = new FileOutputStream(filePath)) {
            fos.write(bytes);
            System.out.println("字节数组已成功写入文件: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
       // pdfGenerator.generatePdfFileByHtmlStr(s,jsonData,outputFile);

//          String htmlStr = "http://salesoss.stbchina.cn/test/template/pod_html/S0X-V1.html";
//        String fileJsonStr = FileUtils.readBufReadFile("D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\test\\POD_H1_data-2.json");
//        JSONObject jsonData1 = JSONObject.parseObject(fileJsonStr);
//        byte[] bytes = generatePdfSteamByHtmlStrAndData(htmlStr, jsonData1);
//        String filePath = "D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\test\\aaa.pdf"; // 输出文件路径
//
//        try (FileOutputStream fos = new FileOutputStream(filePath)) {
//            fos.write(bytes);
//            System.out.println("字节数组已成功写入文件: " + filePath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }

    static String jsonData = "{\n" +
            "    \"amountTotal\": 0,\n" +
            "    \"bstkd\": \"PO230608000235\",\n" +
            "    \"createDateSo\": \"00000000\",\n" +
            "    \"deladdress\": \"北京朝阳区建国门外大街2号银泰中心C座5层-赵璟嘉\",\n" +
            "    \"ebeln\": \"4500812303\",\n" +
            "    \"hkunnr\": \"9000008395\",\n" +
            "    \"hkunnrName\": \"新创建集团别动\",\n" +
            "    \"kunnr\": \"1000494153\",\n" +
            "    \"kunnrName\": \"赛纽仕医药信息咨询（北京）有限公司(BJ)\",\n" +
            "    \"name1Pod\": \"/honda_\",\n" +
            "    \"name2\": \"h3qnmKO0aaELiDjfihkoXQ==\",\n" +
            "    \"poText\": \"打印一下送货单。赵璟嘉\",\n" +
            "    \"reMobNumber\": \"18111246211\",\n" +
            "    \"reTelNumber\": \"18111246211\",\n" +
            "    \"11111\": [\n" +
            "        {\n" +
            "            \"ebelnText\": \"honda_12643126421\",\n" +
            "            \"ematn\": \"1100147160\",\n" +
            "            \"itemNum\": 1,\n" +
            "            \"maktx\": \"史泰博 B-DE1004 白板笔 12支/盒 圆头 黑色\",\n" +
            "            \"meins\": \"EA\",\n" +
            "            \"menge\": \"1.000\",\n" +
            "            \"netAmount\": \"2.16\",\n" +
            "            \"umname\": \"支\",\n" +
            "            \"xsdj\": \"2.50\",\n" +
            "            \"xszj\": \"2.50\",\n" +
            "            \"zzshowName\": \"史泰博 B-DE1004 白板笔 12支/盒 圆头 黑色\"\n" +
            "        },\n" +
            "        {\n" +
            "\n" +
            "        }\n" +
            "    ],\n" +
            "    \"telNumber\": \"18111246211 18111246211\",\n" +
            "    \"totalNum\": 1,\n" +
            "    \"untaxedAmount\": 0\n" +
            "}";
    public static byte[] generatePdfSteamByHtmlStr(String networkHtmlUrl, JSONObject jsonData) {
        if (StringUtils.isBlank(networkHtmlUrl)) {
            return null;
        }
        String htmlStr = HttpUtil.get(networkHtmlUrl);
        return generatePdfSteamByHtmlStrAndData(htmlStr, jsonData);
    }
    public static byte[] generatePdfSteamByHtmlStrAndData(String htmlStr, JSONObject jsonData) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            byteArrayOutputStream = pdfGenerator.generatePdfSteamByHtmlStrAndData(htmlStr, jsonData);
            return byteArrayOutputStream.toByteArray();
        }catch (Exception e){
            logger.error("HTML转pdf异常："+ e.getMessage(),e);
        }finally {
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    logger.error("byteArrayOutputStream 关闭异常：" + e.getMessage(),e);
                }
            }
        }
        return null;
    }
//    public static ByteArrayOutputStream generatePdfSteamByHtmlStrAndData(String htmlStr, JSONObject jsonData) {
//        return pdfGenerator.generatePdfSteamByHtmlStrAndData(htmlStr, jsonData);
//    }
}
